<?php
namespace Admin\Controller;
use Think\Controller;
class GalleryController extends Controller {
    protected $pictureModel;

    public function __construct(){
        parent::__construct();
        $this->pictureModel=D('picture');
        $this->verification();
    }
    //照片列表模板
    public function gallery(){
        if(isset($_GET['aid'])){
            $this->queryfile();
        }else{
            $this->picturelist();
        }
        $this->filecate(1);
        $this->artcate(2);
        $this->albumlist();
        $this->userlist();
        $this->display();
	}

	//照片回收站模板
    public function gallerybin(){
        $this->filecate(1);
        $this->artcate(2);
        $this->albumlist();
        $this->userlist();
        $this->binlist();
        $this->display();
    }

    //验证是否登陆
    protected function verification(){
        $userModel=D('user');
        $userinfo=$userModel->where(array('account'=>cookie('account')))->find();
        if(!che() || !$userinfo){
            $url=U('User/login');
            echo "<script>alert('对不起，请您先登陆。');location.href='$url';</script>";
        }
    }

    //获取搜索栏的相册栏目
    protected function albumlist(){
        $albumModel=D('album');
        $albumlist=$albumModel->select();
        $this->assign('albumlist',$albumlist);
    }

    //获取文件栏目
    protected function filecate($pid){
        $cateModel=D('cate');
        $filecate=$cateModel->gettree($pid);
        $this->assign('filecate',$filecate);
    }

    //获取文章栏目
    protected function artcate($pid){
        $cateModel=D('cate');
        $artcate=$cateModel->gettree($pid);
        $this->assign('artcate',$artcate);
    }

    //第一页与最后一页的提示
    protected function page_tips(){
        $picture_count=count($this->pictureModel->where('is_del=0')->select());
        if(I('get.start')<0){
            echo "<script>alert('已经第一页了');window.location.href=document.referrer;</script>";
        }else if(I('get.start')>$picture_count){
            echo "<script>alert('已经是最后一页了');location.href='javascript:history.go(-1)';</script>";
        }
    }

    //获取照片列表
    protected function picturelist(){
        $this->page_tips();
        $start=isset($_GET['start'])?$_GET['start']+0:0;
        $pagecount = 10;
        $picture_list=$this->pictureModel->where('is_del=0')->order('pid desc')->limit($start . ',' . $pagecount)->select();
        $picture_all=$this->pictureModel->where('is_del=0')->select();
        if($picture_list === false || $picture_all === false) {
            $this->error("获取照片列表失败");
        }else {
            //$picture_rowcount = count($picture_list);
            $picture_count = count($picture_all);
        }
        //$this->assign('picture_rowcount',$picture_rowcount);
        $this->assign('picture_count',$picture_count);
        $this->assign('pagecount',$pagecount);
        $this->assign('picture_list',$picture_list);
        //$this->assign('picture_all',$picture_all);
    }

    //获取用户列表
    protected function userlist(){
        $userModel=D('userinfo');
        $userlist=$userModel->select();
        $this->assign('userlist',$userlist);
    }

    //相册选择查询
    protected function queryfile(){
        $this->page_tips();
        $start=isset($_GET['start'])?$_GET['start']+0:0;
        $pagecount = 10;
        $query_picture=$this->pictureModel->where('is_del=0 and aid=' . I('aid'))->order('pid desc')->limit($start . ',' . $pagecount)->select();
        $query_all=$this->pictureModel->where('is_del=0 and aid=' . I('aid'))->select();
        if($query_picture === false || $query_all === false) {
            $this->error("获取相册照片列表失败");
        }else {
            //$query_rowcount = count($query_picture);
            $query_count = count($query_all);
        }
        //$this->assign('picture_rowcount',$query_rowcount);
        $this->assign('picture_count',$query_count);
        $this->assign('pagecount',$pagecount);
        $this->assign('picture_list',$query_picture);
        //$this->assign('picture_all',$query_all);
    }

    //照片回收站列表
    protected function binlist(){
        $this->page_tips();
        $start=isset($_GET['start'])?$_GET['start']+0:0;
        $pagecount = 10;
        $bin_list=$this->pictureModel->where('is_del=1')->order('pid desc')->limit($start . ',' . $pagecount)->select();
        $bin_all=$this->pictureModel->where('is_del=1')->select();
        if($bin_list === false || $bin_all === false) {
            $this->error("获取照片回收站列表失败");
        }else {
            //$bin_rowcount = count($bin_list);
            $bin_count = count($bin_all);
        }
        //$this->assign('bin_rowcount',$bin_rowcount);
        $this->assign('bin_count',$bin_count);
        $this->assign('pagecount',$pagecount);
        $this->assign('bin_list',$bin_list);
        //$this->assign('bin_all',$bin_all);
    }

    //照片下载
    public function download(){
        if(I('get.pid')){
            $con['pid']=I('get.pid');
            $picture_data=$this->pictureModel->where($con)->select();
            $root_path=$_SERVER['DOCUMENT_ROOT'];
            $picture_path=$picture_data[0]['ori_img'];
            $picture_name=$picture_data[0]['name'];
            $picture=$root_path . '/Public/' . $picture_path;
            import('ORG.Net.Http');
            $http=new \Org\Net\Http;
            $this->pictureModel->where($con)->setInc('down_num');
            $http->download($picture,$picture_name);
        }
    }

    //将文件放入回收站
    public function picturedel(){
        if(I('get.pid')){
            $data['is_del']=1;
            $con['pid']=I('get.pid');
            if($this->pictureModel->where($con)->save($data)){
                echo "<script>alert('成功放入回收站!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
                //$this->redirect("Gallery/gallery/aid/");	//重定向
                //exit;
            }
        }
    }

    //将照片从回收站中还原
    public function binres(){
        if(I('get.pid')){
            $data['is_del']=0;
            $con['pid']=I('get.pid');
            if($this->pictureModel->where($con)->save($data)){
                echo "<script>alert('成功还原照片!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
            }
        }
    }

    //将照片从回收站中删除
    public function bindel(){
        if(I('get.pid')){
            if($this->pictureModel->delete(I('get.pid'))){
                echo "<script>alert('删除成功!');location.href=document.referrer;</script>";
            }
        }
    }

}